package wwk.common.sqlite;

import java.io.File;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author wangweike
 * @create_date 2011-12-22����9:01:35
 * @update_date 2011-12-22����9:01:35
 */

public class SqliteUtil
{
	private SQLiteDatabase sqliteDatabase;
	
	public boolean isDatabaseExists(Context context,String databaseName)
	{
		File dbFile = context.getDatabasePath(databaseName);
		boolean exists=dbFile.exists();
		return exists;
	}
	
	//�����Ƿ�ִ����openOrCreateDatabase
	public boolean openOrCreateDatabase(Context context,String databaseName)
	{
		if(sqliteDatabase==null || !sqliteDatabase.isOpen())
		{
			sqliteDatabase=context.openOrCreateDatabase(databaseName,Context.MODE_PRIVATE, null);
			return true;
		}
		Log.d("database path",sqliteDatabase.getPath());
		return false;
	}
	
	public boolean deleteDatabase(Context context,String databaseName)
	{
		return context.deleteDatabase(databaseName);
	}
	
	public boolean executeSql(String sql)
	{
		boolean ret=false;
		try
		{
 			sqliteDatabase.execSQL(sql);
			ret=true;
		}
		catch(SQLException e)
		{
			ret=false;
		}
		return ret;
	}
	
	 public Cursor query(String table, String[] columns, String selection,
	            String[] selectionArgs, String groupBy, String having,
	            String orderBy) 
	 {
		return sqliteDatabase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
	 }
	 
	 public void close()
	 {
		 if(sqliteDatabase!=null)
		 {
			 sqliteDatabase.close();
			 sqliteDatabase=null;
		 }
	 }
}

